/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.uct.service.plugins  
 */
package com.apache.uct.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.database.constant.Validator;
import com.apache.uct.common.entity.Act;
import com.apache.uct.common.entity.ActGive;
import com.apache.uct.common.entity.Dept;
import com.apache.uct.common.entity.Org;
import com.apache.uct.common.entity.Role;
import com.apache.uct.common.entity.User;
import com.apache.uct.manager.ActGiveManager;
import com.apache.uct.manager.ActManager;
import com.apache.uct.manager.DeptManager;
import com.apache.uct.manager.OrgManager;
import com.apache.uct.manager.RoleManager;
import com.apache.uct.manager.UserManager;

/**
 * description:  执行权限值分配插件
 * @author dwusoft 创建时间：2015-3-12  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
public class ActGiveExecutePlugins implements PluginConnector {

	private ActManager actManager;
	private ActGiveManager actGiveManager;
	private UserManager userManager;
	private RoleManager roleManager;
	private OrgManager orgManager;
	private DeptManager deptManager;

	/**
	 * TODO 执行权限值分配插件 
	 */
	public Object execute(ParamsVo vo) throws Exception {
		/**
		 * 步骤:1、判断权限类型，并获取对应对象信息；
		 *      2、组装权限信息并做保存；
		 */
		String result = "";
		String flag = String.valueOf(vo.getParams("flag"));
		String actId = String.valueOf(vo.getParams("actId"));
		String objId = String.valueOf(vo.getParams("objId"));
		String actGiveType = String.valueOf(vo.getParams("actGiveType"));
		ParamsVo<ActGive> param = new ParamsVo<ActGive>();
		ActGive actGive = new ActGive();
		if ("add".equals(flag)) {
			actGive.setGiveType(actGiveType);
			actGive.setActId(actId);
			buildActInfo(actId, actGive);
			if ("org".equals(actGiveType)) {
				buildOrgInfo(objId, actGive);
			} else if ("user".equals(actGiveType)) {
				buildUserInfo(objId, actGive);
			} else if ("dept".equals(actGiveType)) {
				buildDeptInfo(objId, actGive);
			} else if ("role".equals(actGiveType)) {
				buildRoleInfo(objId, actGive);
			}
			param.setObj(actGive);
			String id = actGiveManager.saveInfo(param);
			if (Validator.isNull(id)) {
				result = "权限分配失败";
			} else {
				ParamsVo avo = new ParamsVo();
				avo.setInfoId(actGive.getActId());
				Act act = (Act) actManager.getInfoById(avo);
				ActCacheHelper.getInstance().writeCache(objId, actGiveType, actGive.getActId(),
						act.getFullEname() + ";" + act.getActUrl(), true);
			}
		} else {
			actGive.setActId(actId);
			actGive.setRoleId(objId);
			param.setObj(actGive);
			boolean mark = actGiveManager.deleteInfo(param);
			if (!mark) {
				result = "权限取消失败";
			} else {
				ActCacheHelper.getInstance().writeCache(objId, actGiveType, actGive.getActId(), "", false);
			}
		}
		return result;
	}

	/**
	 * description:  构建权限信息
	 */
	private void buildActInfo(String actId, ActGive actGive) {
		ParamsVo<Act> vo = new ParamsVo<Act>();
		vo.setInfoId(actId);
		Act act = (Act) actManager.getInfoById(vo);
		actGive.setActId(actId);
		actGive.setSysEname(act.getSysEname());
		actGive.setActFullEname(act.getFullEname());
	}

	/**
	 * description:  构建用户信息
	 */
	private void buildUserInfo(String userId, ActGive actGive) {
		ParamsVo<User> vo = new ParamsVo<User>();
		vo.setInfoId(userId);
		User user = (User) userManager.getInfoById(vo);
		actGive.setRoleId(userId);
		actGive.setRoleEname(user.getUserEname());
	}

	/**
	 * description:  构建机构信息
	 */
	private void buildOrgInfo(String orgId, ActGive actGive) {
		ParamsVo<Org> vo = new ParamsVo<Org>();
		vo.setInfoId(orgId);
		Org org = (Org) orgManager.getInfoById(vo);
		actGive.setRoleId(orgId);
		actGive.setRoleEname(org.getOrgEname());
	}

	/**
	 * description:  构建角色信息
	 */
	private void buildRoleInfo(String roleId, ActGive actGive) {
		ParamsVo<Role> vo = new ParamsVo<Role>();
		vo.setInfoId(roleId);
		Role role = (Role) roleManager.getInfoById(vo);
		actGive.setRoleId(roleId);
		actGive.setRoleEname(role.getRoleEname());
	}

	/**
	 * description:  构建部门信息
	 */
	private void buildDeptInfo(String deptId, ActGive actGive) {
		ParamsVo<Dept> vo = new ParamsVo<Dept>();
		vo.setInfoId(deptId);
		Dept dept = (Dept) deptManager.getInfoById(vo);
		actGive.setRoleId(deptId);
		actGive.setRoleEname(dept.getDeptEname());
	}

	public void setActManager(ActManager actManager) {
		this.actManager = actManager;
	}

	public void setActGiveManager(ActGiveManager actGiveManager) {
		this.actGiveManager = actGiveManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public void setRoleManager(RoleManager roleManager) {
		this.roleManager = roleManager;
	}

	public void setOrgManager(OrgManager orgManager) {
		this.orgManager = orgManager;
	}

	public void setDeptManager(DeptManager deptManager) {
		this.deptManager = deptManager;
	}

}
